// pages/personal/myaddress/myaddress.js
var utils = require('../../../utils/util.js');
var app=getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this;
        console.log(options.typeaddress)
        that.setData({
            typeaddress: options.typeaddress
        })
    },
    onShow:function(){
        
        this.addressList();
    },
    //地址列表
    addressList:function(e){
        var that=this;
        utils.showLoading();
        utils.http("api/v1/address/address_list",{},function(res){
            utils.hideLoading();
            that.setData({
                address:res.datas
            })
        })
    },
    //默认地址
    radioChange:function(e){
        var that=this;
        var id = e.currentTarget.dataset.id;
        var index = e.currentTarget.dataset.index;
        var isdefault=""
        if (e.currentTarget.dataset.isdefault==0){
            isdefault=1
        }else{
            isdefault = 0
        }
        utils.http("/api/v1/address/add_modify_address", {
            id:id,
            is_default:isdefault
        }, function (res) {
            console.log(res);
            if (that.data.typeaddress==0){
                wx.redirectTo({
                   url: '../../confirmorder/confirmorder',
               })
            }
            if (that.data.typeaddress == 1){
                that.addressList();
            }
            that.data.address[index].is_default=1;
            that.setData({
                address: that.data.address
            })
            wx.setStorageSync("defaultAddress", that.data.address[index])
        })

    },
    //跳入新增地址页面
    onAddAddresTap:function(e){
        wx.redirectTo({
            url: '../addaddress/addaddress?typeaddress=' + app.globalData.typeaddress
        })
    },
    //跳入修改地址页面
    oneditAddresTap:function(e){
        var id = e.currentTarget.dataset.id;
        var name = e.currentTarget.dataset.name;
        var address = e.currentTarget.dataset.address;
        var detail = e.currentTarget.dataset.detail;
        var mobile = e.currentTarget.dataset.mobile;
        var isdefault = e.currentTarget.dataset.isdefault;
        var tags = e.currentTarget.dataset.tags;
        wx.redirectTo({
            url: '../addaddress/addaddress?id=' + id + "&name=" + name + "&address=" + address + "&detail=" + detail + "&mobile=" + mobile + "&isdefault=" + isdefault + "&tags=" + tags + "&typeaddress=" + app.globalData.typeaddress
        })
    },
    //删除数据
    delAddresTap:function(e){
        var that=this;
        var id = e.currentTarget.dataset.id;
        var isdefault= e.currentTarget.dataset.isdefault;
        utils.showLoading();
        utils.http("/api/v1/address/del_address", {id:id}, function (res) {
            utils.hideLoading();
            if(res.state==1){
                if(isdefault==1){
                    wx.removeStorage({
                        key: 'defaultAddress',
                        success: function(res) {},
                    })
                }
                wx.showToast({
                    title: '删除成功',
                })
                that.addressList(); 
            }else{
                utils.message(res.datas);
            }
           
        })
    
    }
 
})